// ChildView.cpp : implementation of the CChildView class
//

#include "stdafx.h"
#include "LoPlayer.h"
#include "ChildView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CChildView

CChildView::CChildView()
{
}

CChildView::~CChildView()
{
}

BOOL CChildView::Create(CWnd* pParentWnd, LPCTSTR lpszBkImg, COLORREF crBk/* = 0*/)
{
	if (AfxIsValidString(lpszBkImg)) {
		m_pbmpBkgnd.Attach( Gdiplus::Bitmap::FromFile (lpszBkImg));
	}
	m_clBkgnd = crBk;

	const DWORD dwStyle = WS_POPUP | WS_CLIPCHILDREN | WS_CLIPSIBLINGS;
	LPCTSTR lpszClsView = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS,
						  ::LoadCursor(NULL, IDC_ARROW), 
						  (HBRUSH) ::GetStockObject(NULL_BRUSH), 
						  NULL);


	return CreateEx(0, lpszClsView, NULL, dwStyle, CRect(0, 0, 0, 0), pParentWnd, 0);
}

BEGIN_MESSAGE_MAP(CChildView, CWnd)
	ON_WM_PAINT()
	ON_WM_ERASEBKGND()
END_MESSAGE_MAP()



// CChildView message handlers

void CChildView::OnPaint()
{
	CPaintDC dc(this); // device context for painting

	// TODO: Add your message handler code here
	CRect rect;
	GetClientRect(rect);
	dc.FillSolidRect(rect, m_clBkgnd);

	if (m_pbmpBkgnd) {
		using namespace Gdiplus;

		const int cx = min ((int)m_pbmpBkgnd->GetWidth(), rect.Width());
		const int cy = min ((int)m_pbmpBkgnd->GetHeight(), rect.Height());
		const int x = (rect.Width() - cx) / 2;
		const int y = (rect.Height() - cy) / 2;

		Graphics grapics(dc);
		grapics.DrawImage(m_pbmpBkgnd, Rect(x, y, cx, cy));
	}

	// Do not call CWnd::OnPaint() for painting messages
}

BOOL CChildView::OnEraseBkgnd(CDC* pDC)
{
	// TODO: Add your message handler code here and/or call default
	return FALSE;
	//return CWnd::OnEraseBkgnd(pDC);
}


BOOL CChildView::PreTranslateMessage(MSG* pMsg)
{
	// TODO: Add your specialized code here and/or call the base class
	const UINT message = pMsg->message;
	if (WM_MOUSEFIRST <= message && message <= WM_MOUSELAST) {

		CFrameWnd* pParentWnd = GetParentFrame();
		if (pParentWnd) {
			CPoint p(pMsg->lParam);
			::MapWindowPoints(pMsg->hwnd, pParentWnd->GetSafeHwnd(), &p, 1);
			pParentWnd->PostMessage(message, pMsg->wParam, MAKELPARAM(p.x, p.y));
		}
	}

	return CWnd::PreTranslateMessage(pMsg);
}
